package com.splic.product.service.impl;

import java.util.Date;
import java.util.List;
import com.splic.common.utils.DateUtils;
import com.splic.product.dto.TodayInHistoryRequestDto;
import com.splic.product.service.impl.thirdRequest.TodayInHistoryRequestServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.splic.product.mapper.TodayInHistoryMapper;
import com.splic.product.domain.TodayInHistory;
import com.splic.product.service.TodayInHistoryService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 历史上的今天Service业务层处理
 * 
 * @author singe
 * @date 2022-01-04
 */
@Service
public class TodayInHistoryServiceImpl implements TodayInHistoryService
{
    @Autowired
    private TodayInHistoryMapper todayInHistoryMapper;
    @Autowired
    private TodayInHistoryRequestServiceImpl todayInHistoryRequestServiceImpl;

    /**
     * 查询历史上的今天
     * 
     * @param id 历史上的今天主键
     * @return 历史上的今天
     */
    @Override
    public TodayInHistory selectTodayInHistoryById(Long id)
    {
        return todayInHistoryMapper.selectTodayInHistoryById(id);
    }

    /**
     * 查询历史上的今天列表
     * 
     * @param todayInHistory 历史上的今天
     * @return 历史上的今天
     */
    @Override
    public List<TodayInHistory> selectPageList(TodayInHistory todayInHistory)
    {
        return todayInHistoryMapper.selectTodayInHistoryList(todayInHistory);
    }

    /**
     * 查询历史上的今天列表
     *
     * @param todayInHistory 历史上的今天
     * @return 历史上的今天
     */
    @Override
    public List<TodayInHistory> selectTodayInHistoryList(TodayInHistory todayInHistory)
    {
        return todayInHistoryMapper.selectTodayInHistoryList(todayInHistory);
    }

    /**
     * 新增历史上的今天
     * 
     * @param todayInHistory 历史上的今天
     * @return 结果
     */
    @Override
    public int insert(TodayInHistory todayInHistory)
    {
        todayInHistory.setCreateTime(DateUtils.getNowDate());
        return todayInHistoryMapper.insert(todayInHistory);
    }

    /**
     * 修改历史上的今天
     * 
     * @param todayInHistory 历史上的今天
     * @return 结果
     */
    @Override
    public int update(TodayInHistory todayInHistory)
    {
        todayInHistory.setUpdateTime(DateUtils.getNowDate());
        return todayInHistoryMapper.update(todayInHistory);
    }


    /**
     * 删除历史上的今天信息
     * 
     * @param id 历史上的今天主键
     * @return 结果
     */
    @Override
    public int deleteById(Long id)
    {
        return todayInHistoryMapper.deleteById(id);
    }
    /**
     * 修改历史上的今天
     *
     * @param todayInHistory 历史上的今天信息
     * @return 结果
     */
    @Override
    @Transactional
    public int saveOrUpdate(TodayInHistory todayInHistory)
    {
        if(todayInHistory.getId() != null){
            todayInHistory.setUpdateTime(new Date());
            return todayInHistoryMapper.update(todayInHistory);
        }else{
            todayInHistory.setCreateTime(new Date());
            return todayInHistoryMapper.insert(todayInHistory);
        }
    }

    @Override
    public void requestThirdData() {
        TodayInHistoryRequestDto dto = todayInHistoryRequestServiceImpl.requestThirdData();
        if(dto != null && CollectionUtils.isNotEmpty(dto.getTodayInHistoryList())){
            for(TodayInHistory todayInHistory :dto.getTodayInHistoryList()){
                saveOrUpdate(todayInHistory);
            }
        }
    }
}
